package com.tkgarment.system.action;
 
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
import com.tkgarment.system.dao.DataFileDAO;
import com.tkgarment.system.domain.DataFile;
import com.tkgarment.system.factory.DAOFactory;
import java.util.Date;
 
public class UserFileAction extends ActionSupport implements
        ServletRequestAware {
     private File fileUpload;
     private String fileUploadContentType;
     private String fileUploadFileName;
    private String empId;
    private HttpServletRequest servletRequest;
    private String fileType;
   private Integer activeTab;
    
    public String execute() {
        try {
            activeTab = 2;
            String filePath = "d:/tksystem/upload/";
//            String filePath = servletRequest.getSession().getServletContext().getRealPath("/files");
            System.out.println("Server path:" + filePath);
            File fileToCreate = new File(filePath, this.fileUploadFileName);
            File fl = new File(filePath);
            if(!fl.exists()){
                fl.mkdirs();
            }
            FileUtils.copyFile(this.fileUpload, fileToCreate);
//            System.out.println(this.fileUpload.getCanonicalPath());
//            System.out.println(this.fileUpload.getAbsolutePath());
//            System.out.println(this.fileUpload.getPath());
//            System.out.println(this.fileUpload.toString());
            
            DataFileDAO eDao = DAOFactory.createDataFileDao();
            DataFile f = new DataFile();
            
            empId =   servletRequest.getSession().getAttribute("session_emp_id").toString();
             f.setCreateBy("admin");
             f.setEmpId(empId);
             f.setFileName(this.fileUploadFileName);
             f.setFileType(fileType);
             eDao.insertSingle(f);
        } catch (Exception e) {
            e.printStackTrace();
            
            addActionError(e.getMessage());
 
            return INPUT;
        }
        return SUCCESS;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

 

    public Integer getActiveTab() {
        return activeTab;
    }

    public void setActiveTab(Integer activeTab) {
        this.activeTab = activeTab;
    }

    public File getFileUpload() {
        return fileUpload;
    }

    public void setFileUpload(File fileUpload) {
        this.fileUpload = fileUpload;
    }

    public String getFileUploadContentType() {
        return fileUploadContentType;
    }

    public void setFileUploadContentType(String fileUploadContentType) {
        this.fileUploadContentType = fileUploadContentType;
    }

    public String getFileUploadFileName() {
        return fileUploadFileName;
    }

    public void setFileUploadFileName(String fileUploadFileName) {
        this.fileUploadFileName = fileUploadFileName;
    }

 
    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
 
    }

    public String getFileType() {
        return fileType;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }
    
    
}